<?php
namespace app\common\model\business;

use think\Model;
use traits\model\SoftDelete;

class Address extends Model
{
    use SoftDelete;

    protected $name = 'business_address';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;

    // 软删除字段
    protected $deleteTime = 'deletetime';

    // 追加数据表中不存在的字段
    protected $append = [
        'region_text',
    ];

    public function getRegionTextAttr($value, $data)
    {
        $region_text = '';

        $province = model('Region')->where(['code' => $data['province']])->value('name');
        $city = model('Region')->where(['code' => $data['city']])->value('name');
        $district = model('Region')->where(['code' => $data['district']])->value('name');

        if ($province) {
            $region_text = $province;
        }

        if ($city) {
            $region_text .= $city;
        }

        if ($district) {
            $region_text .= $district;
        }

        return $region_text;
    }
}